package psn.kiko.service.business.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import psn.kiko.mapper.business.system.BlogMapper;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.dto.BlogDTO;
import psn.kiko.pojo.dto.FileDTO;
import psn.kiko.pojo.entity.system.BlogEntity;
import psn.kiko.pojo.vo.BlogVO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 博客服务
 */
@Service
public class BlogService{
	@Resource
	BlogMapper blogMapper;
	@Resource
	FileService fileService;
	
	@Transactional
	public Integer addBlog(BlogDTO blogDTO){
		BlogEntity blogEntity = new BlogEntity();
		deleteUnUsedFilesOfBlog(blogDTO);
		setBlogFileIds(blogDTO,blogEntity);
		//添加博客
		BeanUtils.copyProperties(blogDTO,blogEntity);
		return blogMapper.insert(blogEntity);
	}
	
	/**
	 * 设置博客内所有文件的id
	 */
	private void setBlogFileIds(BlogDTO blogDTO,BlogEntity blogEntity){
		List<String> usedFileSrcList = blogDTO.getUsedFileSrcList();
		List<FileDTO> usedFiles = null;
		if(usedFileSrcList.size() > 0){
			//查询使用的文件信息
			usedFiles = fileService.getFilesBySrc(usedFileSrcList);
		}
		if(usedFiles != null&&usedFiles.size() > 0){
			List<Long> fileIds = usedFiles.stream()
					.map(FileDTO::getId)
					.collect(Collectors.toList());
			StringBuilder sb = new StringBuilder();
			for(Long fileId: fileIds){
				sb.append(fileId).append(',');
			}
			String srcWithComma = sb.toString();
			srcWithComma = srcWithComma.substring(0,srcWithComma.lastIndexOf(','));
			//博客内所有文件的id，以逗号分隔
			blogEntity.setFileIds(srcWithComma);
		}
	}
	
	/**
	 * 删除无用的文件
	 */
	private void deleteUnUsedFilesOfBlog(BlogDTO blogDTO){
		//删除无用的图片资源
		List<String> unUsedFileSrcList = blogDTO.getUnUsedFileSrcList();
		if(unUsedFileSrcList.size() > 0){
			List<FileDTO> fileDTOS = fileService.getFilesBySrc(unUsedFileSrcList);
			fileService.deleteByDtos(fileDTOS);
		}
	}
	
	@Transactional
	public Integer deleteBlogs(List<Integer> blogIds){
		List<String> imageSrcList = blogMapper.selectImageIdsById(blogIds);
		//博客内图片文件的id集合
		ArrayList<Long> fileIds = new ArrayList<>();
		imageSrcList.forEach(imageSrcs->{
			String[] ids = imageSrcs.split(",");
			for(String id: ids){
				fileIds.add(Long.valueOf(id));
			}
		});
		//查询文件信息
		List<FileDTO> fileDTOS = fileService.getFileByIds(fileIds);
		//删除文件信息、及物理文件
		if(fileDTOS.size()>0){
			fileService.deleteByDtos(fileDTOS);
		}
		//删除博客
		return blogMapper.deleteBatchById(blogIds);
	}
	
	public BlogVO getBlogById(Integer blogId){
		return blogMapper.selectById(blogId);
	}
	
	public PageResult<Object> pagingGetBlogs(int pageNum,int pageSize){
		Page<Object> page = PageHelper.startPage(pageNum,pageSize);
		List<BlogVO> blogVOList = blogMapper.selectList();
		PageResult<Object> pageResult = PageResult.builder()
				.total(((int) page.getTotal()))
				.data(blogVOList)
				.build();
		return pageResult;
	}
	
	public Integer updateBlog(BlogDTO blogDTO){
		BlogEntity blogEntity = new BlogEntity();
		deleteUnUsedFilesOfBlog(blogDTO);
		setBlogFileIds(blogDTO,blogEntity);
		BeanUtils.copyProperties(blogDTO,blogEntity);
		return blogMapper.updateById(blogEntity);
	}
	
	public List<String> batchAddFile(MultipartFile[] files){
		return fileService.batchAddFile(files);
	}
}
